package com.lyhengtongwl.zqsnews.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.Constant;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.widget.AdTimer;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 广告页
 */
public class AdvertActivity extends AppCompatActivity {

    @BindView(R.id.iv_adpic)
    ImageView ivAdpic;
    @BindView(R.id.tv_adtime)
    TextView tvAdtime;

    AdTimer adTimer;
    String title = "";
    String url = "";
    String actionType = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advert);
        ButterKnife.bind(this);

        String imageUrl = SPUtils.get(getApplicationContext(), Constant.AdvertInfo.imageUrl, "").toString();
        Glide.with(this).load(imageUrl).into(ivAdpic);

        title = SPUtils.get(getApplicationContext(), Constant.AdvertInfo.title, "").toString();
        url = SPUtils.get(getApplicationContext(), Constant.AdvertInfo.actionUrl, "").toString();
        actionType = SPUtils.get(getApplicationContext(), Constant.AdvertInfo.actionType, "").toString();

        adTimer = new AdTimer(this, tvAdtime);
        adTimer.start();
    }

    @OnClick({R.id.iv_adpic, R.id.tv_adtime})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.iv_adpic:
                if ("toURL".equals(actionType)) {
                    startActivity(new Intent(this, WebActivity.class)
                            .putExtra("url", url)
                            .putExtra("title", title)
                            .putExtra("referer", "AdvertActivity"));
                    if (adTimer != null) {
                        adTimer.cancel();
                    }
                    finish();
                }
                break;
            case R.id.tv_adtime:
                if (adTimer != null) {
                    adTimer.cancel();
                    startActivity(new Intent(this, MainActivity.class));
                    finish();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adTimer != null) {
            adTimer.cancel();
        }
    }
}
